//
//  FloatClockView.swift
//  FloatClock
//
//  Created by runbey_dev_ios on 2023/6/19.
//

import UIKit
import SnapKit

class ClockView: UIView {

    lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.textColor = UIColor.black;
        label.font = UIFont.systemFont(ofSize: 25)
        label.baselineAdjustment = .alignCenters
        return label
    }()
        
    lazy var fpsLabel : FPSLabel = {
        let label = FPSLabel()
        label.textAlignment = .left
        label.textColor = UIColor.black;
        label.font = UIFont.systemFont(ofSize: 14)
        label.adjustsFontSizeToFitWidth = true
        label.baselineAdjustment = .alignCenters
        return label
    }()
    
    lazy var cpuLabel : CPULabel = {
        let label = CPULabel()
        label.isHidden = true
        label.textAlignment = .center
        label.textColor = UIColor.black;
        label.font = UIFont.systemFont(ofSize: 25)
        label.adjustsFontSizeToFitWidth = true
        label.baselineAdjustment = .alignCenters
        return label
    }()
    
    lazy var netUpSpeedLabel : UILabel = {
        let label = UILabel()
        label.textAlignment = .right
        label.textColor = UIColor.black;
        label.font = UIFont.systemFont(ofSize: 14)
        label.adjustsFontSizeToFitWidth = true
        label.baselineAdjustment = .alignCenters
        return label
    }()
    
    lazy var netDownSpeedLabel : UILabel = {
        let label = UILabel()
        label.textAlignment = .right
        label.textColor = UIColor.black;
        label.font = UIFont.systemFont(ofSize: 14)
        label.adjustsFontSizeToFitWidth = true
        label.baselineAdjustment = .alignCenters
        return label
    }()
    
    //展示样式
    var displayStyle : Int = 0 {
        didSet {
            if displayStyle == 0 {
                timeLabel.isHidden = false;
                fpsLabel.isHidden = false;
                cpuLabel.isHidden = true
                fpsLabel.textAlignment = .left
                if showStyle == 0 {
                    fpsLabel.font = UIFont.systemFont(ofSize: 16)
                    netUpSpeedLabel.font = UIFont.systemFont(ofSize: 16)
                    netDownSpeedLabel.font = UIFont.systemFont(ofSize: 16)
                    fpsLabel.snp.remakeConstraints { make in
                        make.bottom.equalTo(-12);
                        make.width.equalToSuperview().multipliedBy(100/353.0)
                        make.left.equalTo(12);
                        make.height.equalTo(timeLabel.snp.width).multipliedBy(20.0/293.0)
                    }
                } else {
                    fpsLabel.font = UIFont.systemFont(ofSize: 11)
                    netUpSpeedLabel.font = UIFont.systemFont(ofSize: 11)
                    netDownSpeedLabel.font = UIFont.systemFont(ofSize: 11)
                    fpsLabel.snp.remakeConstraints { make in
                        make.centerY.equalToSuperview();
                        make.width.equalToSuperview().multipliedBy(100/353.0)
                        make.left.equalTo(12);
                        make.height.equalTo(timeLabel.snp.width).multipliedBy(20.0/293.0)
                    }
                }
            }  else if displayStyle == 1 {
                timeLabel.isHidden = true
                fpsLabel.isHidden = false
                cpuLabel.isHidden = false
                fpsLabel.textAlignment = .left
                if showStyle == 0 {
                    fpsLabel.font = UIFont.systemFont(ofSize: 16)
                    netUpSpeedLabel.font = UIFont.systemFont(ofSize: 16)
                    netDownSpeedLabel.font = UIFont.systemFont(ofSize: 16)
                    fpsLabel.snp.remakeConstraints { make in
                        make.bottom.equalTo(-12);
                        make.width.equalToSuperview().multipliedBy(100/353.0)
                        make.left.equalTo(12);
                        make.height.equalTo(timeLabel.snp.width).multipliedBy(20.0/293.0)
                    }
                } else {
                    fpsLabel.font = UIFont.systemFont(ofSize: 11)
                    netUpSpeedLabel.font = UIFont.systemFont(ofSize: 11)
                    netDownSpeedLabel.font = UIFont.systemFont(ofSize: 11)
                    fpsLabel.snp.remakeConstraints { make in
                        make.centerY.equalToSuperview();
                        make.width.equalToSuperview().multipliedBy(100/353.0)
                        make.left.equalTo(12);
                        make.height.equalTo(timeLabel.snp.width).multipliedBy(20.0/293.0)
                    }
                }
            } else {
                timeLabel.isHidden = true;
                fpsLabel.isHidden = false;
                cpuLabel.isHidden = true
                fpsLabel.font = UIFont.systemFont(ofSize: 25)
                fpsLabel.textAlignment = .center
                fpsLabel.snp.remakeConstraints { make in
                    make.left.equalTo(30)
                    make.right.equalTo(-30)
                    make.centerY.equalToSuperview()
                }
            }
        }
    }
    
    //画中画样式调整
    var showStyle: Int = 0 {
        didSet {
            if showStyle == 0 {
                if displayStyle == 0 {
                    fpsLabel.font = UIFont.systemFont(ofSize: 16)
                    fpsLabel.textAlignment = .left
                    timeLabel.isHidden = false;
                    cpuLabel.isHidden = true;
                    fpsLabel.isHidden = false;
                    fpsLabel.snp.remakeConstraints { make in
                        make.bottom.equalTo(-12);
                        make.width.equalToSuperview().multipliedBy(100/353.0)
                        make.left.equalTo(12);
                        make.height.equalTo(timeLabel.snp.width).multipliedBy(20.0/293.0)
                    }
                } else if displayStyle == 1 {
                    fpsLabel.font = UIFont.systemFont(ofSize: 16)
                    fpsLabel.textAlignment = .left
                    timeLabel.isHidden = true;
                    cpuLabel.isHidden = false;
                    fpsLabel.isHidden = false;
                    fpsLabel.snp.remakeConstraints { make in
                        make.bottom.equalTo(-12);
                        make.width.equalToSuperview().multipliedBy(100/353.0)
                        make.left.equalTo(12);
                        make.height.equalTo(timeLabel.snp.width).multipliedBy(20.0/293.0)
                    }
                }else {
                    fpsLabel.font = UIFont.systemFont(ofSize: 25)
                    fpsLabel.textAlignment = .center
                    timeLabel.isHidden = true;
                    cpuLabel.isHidden = true;
                    fpsLabel.isHidden = false;
                    fpsLabel.snp.remakeConstraints { make in
                        make.left.equalTo(30)
                        make.right.equalTo(-30)
                        make.centerY.equalToSuperview()
                    }
                }
                netUpSpeedLabel.font = UIFont.systemFont(ofSize: 16)
                netDownSpeedLabel.font = UIFont.systemFont(ofSize: 16)
                
                netUpSpeedLabel.snp.remakeConstraints { make in
                    make.width.equalToSuperview().multipliedBy(100/353.0)
                    make.bottom.equalTo(netDownSpeedLabel.snp.top).offset(-2);
                    make.right.equalTo(-12)
                    make.height.equalTo(timeLabel.snp.width).multipliedBy(20.0/293.0)
                }
                
                netDownSpeedLabel.snp.makeConstraints { make in
                    make.bottom.equalTo(-12);
                    make.width.equalToSuperview().multipliedBy(100/353.0)
                    make.right.equalTo(-12)
                    make.height.equalTo(timeLabel.snp.width).multipliedBy(20.0/293.0)
                }
                
            } else {
                if displayStyle == 0 {
                    fpsLabel.font = UIFont.systemFont(ofSize: 16)
                    fpsLabel.textAlignment = .left
                    timeLabel.isHidden = false;
                    cpuLabel.isHidden = true;
                    fpsLabel.isHidden = false;
                    fpsLabel.snp.remakeConstraints { make in
                        make.centerY.equalToSuperview();
                        make.width.equalToSuperview().multipliedBy(100/353.0)
                        make.left.equalTo(12);
                        make.height.equalTo(timeLabel.snp.width).multipliedBy(20.0/293.0)
                    }
                } else if displayStyle == 1 {
                    fpsLabel.font = UIFont.systemFont(ofSize:16)
                    fpsLabel.textAlignment = .left
                    timeLabel.isHidden = true;
                    cpuLabel.isHidden = false;
                    fpsLabel.isHidden = false;
                    fpsLabel.snp.remakeConstraints { make in
                        make.centerY.equalToSuperview();
                        make.width.equalToSuperview().multipliedBy(100/353.0)
                        make.left.equalTo(12);
                        make.height.equalTo(timeLabel.snp.width).multipliedBy(20.0/293.0)
                    }
                }else {
                    fpsLabel.font = UIFont.systemFont(ofSize: 25)
                    fpsLabel.textAlignment = .center
                    timeLabel.isHidden = true;
                    cpuLabel.isHidden = true;
                    fpsLabel.isHidden = false;
                    fpsLabel.snp.remakeConstraints { make in
                        make.left.equalTo(30)
                        make.right.equalTo(-30)
                        make.centerY.equalToSuperview()
                    }
                }
                netUpSpeedLabel.font = UIFont.systemFont(ofSize: 11)
                netDownSpeedLabel.font = UIFont.systemFont(ofSize: 11)
 
                netUpSpeedLabel.snp.remakeConstraints { make in
                    make.width.equalToSuperview().multipliedBy(100/353.0)
                    make.bottom.equalTo(0)
                    make.right.equalTo(-12)
                    make.height.equalTo(timeLabel.snp.width).multipliedBy(20.0/293.0)
                }
                
                netDownSpeedLabel.snp.makeConstraints { make in
                    make.bottom.equalTo(0)
                    make.width.equalToSuperview().multipliedBy(100/353.0)
                    make.right.equalTo(-12)
                    make.height.equalTo(timeLabel.snp.width).multipliedBy(20.0/293.0)
                }
                
            }
        }
    }

    var isShowMilsecond: Bool = false {
        didSet {
            if isShowMilsecond {
                timeLabel.snp.remakeConstraints { (make) in
                    make.centerX.equalToSuperview()
                    make.width.equalToSuperview().multipliedBy(293.0/353.0)
                    make.centerY.equalToSuperview()
                    make.height.equalTo(timeLabel.snp.width).multipliedBy(62.0/293.0)
                }
            } else {
                timeLabel.snp.remakeConstraints { (make) in
                    make.centerX.equalToSuperview()
                    make.width.equalToSuperview().multipliedBy(195.0/353.0)
                    make.centerY.equalToSuperview()
                    make.height.equalTo(timeLabel.snp.width).multipliedBy(62.0/195.0)
                }
            }
        }
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        backgroundColor = UIColor.lightGray
        addSubview(timeLabel)
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(30)
            make.right.equalTo(-30)
            make.centerY.equalToSuperview()
        }
        
        addSubview(cpuLabel)
        cpuLabel.snp.makeConstraints { (make) in
            make.left.equalTo(30)
            make.right.equalTo(-30)
            make.centerY.equalToSuperview()
        }
        
        addSubview(fpsLabel)
        fpsLabel.snp.makeConstraints { make in
            make.bottom.equalTo(-12);
            make.width.equalToSuperview().multipliedBy(100/353.0)
            make.left.equalTo(12);
            make.height.equalTo(timeLabel.snp.width).multipliedBy(20.0/293.0)
        }
        
        addSubview(netDownSpeedLabel)
        netDownSpeedLabel.snp.makeConstraints { make in
            make.bottom.equalTo(-12);
            make.width.equalToSuperview().multipliedBy(100/353.0)
            make.right.equalTo(-12)
            make.height.equalTo(timeLabel.snp.width).multipliedBy(20.0/293.0)
        }
        
        addSubview(netUpSpeedLabel)
        netUpSpeedLabel.snp.makeConstraints { make in
            make.width.equalToSuperview().multipliedBy(100/353.0)
            make.bottom.equalTo(netDownSpeedLabel.snp.top).offset(-2);
            make.right.equalTo(-12)
            make.height.equalTo(timeLabel.snp.width).multipliedBy(20.0/293.0)
        }
    }
    
}
